/**************************************************************************//**
 * @file     gpio.h
 * @version  V1.00
 * $Revision: 9 $
 * $Date: 15/05/18 5:38p $
 * @brief    NUC970 GPIO driver header file
 *
 * @note
 * SPDX-License-Identifier: Apache-2.0
 * Copyright (C) 2015 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/

#ifndef __NUC90_GPIO_H__
#define __NUC90_GPIO_H__

#include "nuc970.h"

#ifdef __cplusplus
extern "C"
{
#endif


/** @addtogroup NUC970_Device_Driver NUC970 Device Driver
  @{
*/

/** @addtogroup NUC970_GPIO_Driver GPIO Driver
  @{
*/

/** @addtogroup NUC970_GPIO_EXPORTED_CONSTANTS GPIO Exported Constants
  @{
*/

/*---------------------------------------------------------------------------------------------------------*/
/*  MODE Constant Definitions                                                                              */
/*---------------------------------------------------------------------------------------------------------*/
/// @cond HIDDEN_SYMBOLS
#ifndef GPIO_ERR_PORT_BUSY
  #define GPIO_ERR_PORT_BUSY      -1
  #define GPIO_ERR_UNSUPPORTED    -2
  #define GPIO_ERR_BIT_BUSY       -3
  #define SUCCESSFUL              0
#endif
/// @endcond HIDDEN_SYMBOLS

#define GPIOA_MASK  0x0000FFFF  /*!< GPIO Port A Mask */
#define GPIOB_MASK  0x0000FFFF  /*!< GPIO Port B Mask */
#define GPIOC_MASK  0x00007FFF  /*!< GPIO Port C Mask */
#define GPIOD_MASK  0x0000FFFF  /*!< GPIO Port D Mask */
#define GPIOE_MASK  0x0000FFFF  /*!< GPIO Port E Mask */
#define GPIOF_MASK  0x0000FFFF  /*!< GPIO Port F Mask */
#define GPIOG_MASK  0x0000FFFF  /*!< GPIO Port G Mask */
#define GPIOH_MASK  0x0000FFFF  /*!< GPIO Port H Mask */
#define GPIOI_MASK  0x0000FFFF  /*!< GPIO Port I Mask */
#define GPIOJ_MASK  0x0000003F  /*!< GPIO Port J Mask */

/// @cond HIDDEN_SYMBOLS
typedef INT32 (*GPIO_CALLBACK)(UINT32 status, UINT32 userData);
typedef INT32 (*EINT_CALLBACK)(UINT32 status, UINT32 userData);
/// @endcond HIDDEN_SYMBOLS

/** \brief  Structure type of GPIO_INDEX
 */
typedef enum
{
    NU_PORTA,
    NU_PORTB,
    NU_PORTC,
    NU_PORTD,
    NU_PORTE,
    NU_PORTF,
    NU_PORTG,
    NU_PORTH,
    NU_PORTI,
    NU_PORTJ,
    NU_PORT_MAX,
} GPIO_INDEX;

/** \brief  Structure type of GPIO_DIR
 */
typedef enum {
    GPIO_DIR_INPUT,     /*!< GPIO Output mode      */
    GPIO_DIR_OUTPUT     /*!< GPIO Input mode      */
} GPIO_DIR;

/** \brief  Structure type of GPIO_PULL
 */
typedef enum {
    GPIO_PULL_NO,       /*!< GPIO Pull-Up Disable */
    GPIO_PULL_UP,       /*!< GPIO Pull-Up Enable */
    GPIO_PULL_DOWN      /*!< GPIO Pull-Down Enable */
} GPIO_PULL;

/** \brief  Structure type of GPIO_DRV
 */
typedef enum {
    GPIO_DRV_LOW,       /*!< GPIO Set to Low */
    GPIO_DRV_HIGH       /*!< GPIO Set to High */
} GPIO_DRV;

/** \brief  Structure type of GPIO_NIRQ
 */
typedef enum {
    GPIO_NIRQ0 = 0,     /*!< External interrupt 0 */
    GPIO_NIRQ1,         /*!< External interrupt 1 */
    GPIO_NIRQ2,         /*!< External interrupt 2 */
    GPIO_NIRQ3,         /*!< External interrupt 3 */
    GPIO_NIRQ4,         /*!< External interrupt 4 */
    GPIO_NIRQ5,         /*!< External interrupt 5 */
    GPIO_NIRQ6,         /*!< External interrupt 6 */
    GPIO_NIRQ7,         /*!< External interrupt 7 */
} GPIO_NIRQ;

/** \brief  Structure type of GPIO_TRIGGER_TYPE
 */
typedef enum {
    GPIO_TRIGGER_LOW,                   /*!< Trigger type set low */
    GPIO_TRIGGER_HIGH,                  /*!< Trigger type set high */
    GPIO_TRIGGER_FALLING,               /*!< Trigger type set falling edge */
    GPIO_TRIGGER_RISING,                /*!< Trigger type set rising edge */
    GPIO_TRIGGER_BOTH_EDGE              /*!< Trigger type set falling edge and rising edge */
} GPIO_TRIGGER_TYPE;

/// @cond HIDDEN_SYMBOLS
typedef struct
{
    __IO UINT32 DIR;                    /*!< GPIO direction control register */
    __IO UINT32 DATAOUT;                /*!< GPIO data output register */
    __IO UINT32 DATAIN;                 /*!< GPIO data input register */
    __IO UINT32 IMD;                    /*!< GPIO Interrupt Mode Register */
    __IO UINT32 IREN;                   /*!< GPIO Interrupt Rising-Edge or Level-High Enable Register */
    __IO UINT32 IFEN;                   /*!< GPIO Interrupt Falling-Edge or Level-Low Enable Register */
    __IO UINT32 ISR;                    /*!< GPIO Interrupt Status Register */
    __IO UINT32 DBEN;                   /*!< GPIO De-bounce Enable Register */
    __IO UINT32 PUEN;                   /*!< GPIO Pull-Up Enable Register */
    __IO UINT32 PDEN;                   /*!< GPIO Pull-Down Enable Register */
    __IO UINT32 ICEN;                   /*!< GPIO CMOS Input Enable Register */
    __IO UINT32 ISEN;                   /*!< GPIO Schmitt-Trigger Input Enable Register */
} GPIO_TypeDef;

typedef struct 
{
    UINT16          bitBusyFlag[NU_PORT_MAX];
    GPIO_CALLBACK   IRQCallback[NU_PORT_MAX];
    UINT32          IRQUserData[NU_PORT_MAX];
    EINT_CALLBACK   EINTIRQCallback[8];
    UINT32          EINTIRQUserData[8];
} GPIO_CFG;
/// @endcond HIDDEN_SYMBOLS

#define GPIOA    ((GPIO_TypeDef*)GPIOA_BA)
#define GPIOB    ((GPIO_TypeDef*)GPIOB_BA)
#define GPIOC    ((GPIO_TypeDef*)GPIOC_BA)
#define GPIOD    ((GPIO_TypeDef*)GPIOD_BA)
#define GPIOE    ((GPIO_TypeDef*)GPIOE_BA)
#define GPIOF    ((GPIO_TypeDef*)GPIOF_BA)
#define GPIOG    ((GPIO_TypeDef*)GPIOG_BA)
#define GPIOH    ((GPIO_TypeDef*)GPIOH_BA)
#define GPIOI    ((GPIO_TypeDef*)GPIOI_BA)
#define GPIOJ    ((GPIO_TypeDef*)GPIOJ_BA)

/*@}*/ /* end of group NUC970_GPIO_EXPORTED_CONSTANTS */


/** @addtogroup NUC970_GPIO_EXPORTED_FUNCTIONS GPIO Exported Functions
  @{
*/

INT32 GPIO_Open(GPIO_TypeDef *port, GPIO_DIR direction, GPIO_PULL pull);
INT32 GPIO_Close(GPIO_TypeDef *port);
INT32 GPIO_Set(GPIO_TypeDef *port, UINT32 bitMap);
INT32 GPIO_Clr(GPIO_TypeDef *port, UINT32 bitMap);
UINT32 GPIO_ReadPort(GPIO_TypeDef *port);
INT32 GPIO_SetPortDir(GPIO_TypeDef *port, GPIO_DIR direction);

/* General GPIO bit function */
INT32 GPIO_OpenBit(GPIO_TypeDef *port, UINT32 bit, GPIO_DIR direction, GPIO_PULL pull);
INT32 GPIO_CloseBit(GPIO_TypeDef *port, UINT32 bit);
INT32 GPIO_SetBit(GPIO_TypeDef *port, UINT32 bit);
INT32 GPIO_ClrBit(GPIO_TypeDef *port, UINT32 bit);
INT32 GPIO_ReadBit(GPIO_TypeDef *port, UINT32 bit);
INT32 GPIO_SetBitDir(GPIO_TypeDef *port, UINT32 bit, GPIO_DIR direction);
INT32 GPIO_EnableTriggerType(GPIO_TypeDef *port, UINT32 bit, GPIO_TRIGGER_TYPE triggerType);
INT32 GPIO_DisableTriggerType(GPIO_TypeDef *port, UINT32 bit);
INT32 GPIO_EnableInt(GPIO_TypeDef *port, GPIO_CALLBACK callback, UINT32 userData);
INT32 GPIO_DisableInt(GPIO_TypeDef *port);
INT32 GPIO_ClrISR(GPIO_TypeDef *port, UINT32 bit);
INT32 GPIO_ClrISRBit(GPIO_TypeDef *port, UINT32 bit);

/* GPIO status function */
BOOL GPIO_BitIsUsed(GPIO_TypeDef *port, UINT32 bit);

/* External GPIO interrupt function */
INT32 GPIO_EnableEINT(GPIO_NIRQ nIrq, GPIO_CALLBACK callback, UINT32 userData);
INT32 GPIO_DisableEINT(GPIO_NIRQ nIrq);
INT32 GPIO_EnableDebounce(INT32 debounceClkSel);
INT32 GPIO_DisableDebounce(void);

/*@}*/ /* end of group NUC970_GPIO_EXPORTED_FUNCTIONS */

/*@}*/ /* end of group NUC970_GPIO_Driver */

/*@}*/ /* end of group NUC970_Device_Driver */

#ifdef __cplusplus
}
#endif

#endif // __NUC90_GPIO_H__

/*** (C) COPYRIGHT 2015 Nuvoton Technology Corp. ***/
